package com.puccampinas.finalproject.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.eaio.uuid.UUID;
import com.google.gson.Gson;
import com.puccampinas.finalproject.core.controller.UserController;
import com.puccampinas.finalproject.core.persistence.entity.Partner;
import com.puccampinas.finalproject.rest.to.RequestResponse;
import com.puccampinas.finalproject.rest.to.ResponseStatus;

@Path("/api/parceiros")
public class PartnerHandler {


	@GET
    @Path("acompanhar/")
    @Produces({MediaType.APPLICATION_JSON})
	public String acompanhar(@QueryParam("usuario") Long usuario, @QueryParam("partnerId") Long partnerId, @QueryParam("callback") String callback) {
		
		String ticket = new UUID().toString();
		RequestResponse rp = new RequestResponse();
		rp.setTicket(ticket);
		rp.setDate(new Date());
		rp.setStatus(ResponseStatus.ERROR);
		rp.setMessage("ERROR");
		
		if (usuario != null && partnerId != null) {
			Boolean result = UserController.acompanhar(usuario, partnerId);
			if (result) {
				rp.setStatus(ResponseStatus.OK);
				rp.setMessage("SUCCESS");
			}
		} 
		
		Gson gson = new Gson();
		String json = gson.toJson(rp);
		
		return callback + "(" + json + ")";
	}
	
	@GET
    @Path("desacompanhar/")
    @Produces({MediaType.APPLICATION_JSON})
	public String desacompanhar(@QueryParam("usuario") Long usuario, @QueryParam("partnerId") Long partnerId, @QueryParam("callback") String callback) {
		
		String ticket = new UUID().toString();
		RequestResponse rp = new RequestResponse();
		rp.setTicket(ticket);
		rp.setDate(new Date());
		rp.setStatus(ResponseStatus.ERROR);
		rp.setMessage("ERROR");
		
		if (usuario != null && partnerId != null) {
			Boolean result = UserController.desacompanhar(usuario, partnerId);
			if (result) {
				rp.setStatus(ResponseStatus.OK);
				rp.setMessage("SUCCESS");
			}
		} 
		Gson gson = new Gson();
		String json = gson.toJson(rp);
		
		return callback + "(" + json + ")";
	}
	
	@GET
    @Path("getPartners/")
    @Produces({MediaType.APPLICATION_JSON})
	public String getPartners(@QueryParam("usuario") String usuario, @QueryParam("callback") String callback) {
		List<Partner> list = new ArrayList<Partner>();
		
		if (usuario != null) {
			list = UserController.listPartners(usuario);
		}
		
		Gson gson = new Gson();
		String json = gson.toJson(list);
		
		return callback + "({\"parceiros\": "+ json + "})";
	}
	
}
